#!/bin/bash

[ $# != 1 ] && {  
echo "用法: bash $0 init | restart | rm " 
echo "比如: bash $0 init" 
exit 1; 
} 
echo "[INFO] 执行命令 $0 $1 "


[ "$1" == "init" ] && {


echo "[INFO] 正在初始化docker运行容器"

read -p "[INFO] 请输入mysql数据库容器的名称:" database
echo -e "\n"
echo "[INFO] mysql数据库容器: $database"


[ -z "$database" ] && {
	echo "[INFO] mysql数据库容器的名称不能为空"
	exit 1;
}

read -p "[INFO] 请输入apache容器的名称:" app
echo -e "\n"
echo "[INFO] mysql数据库容器: $app"

[ -z "$app" ] && {
	echo "[INFO] apache容器的名称不能为空"
	exit 1;
}

# init mysql
docker run --name $database \
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_DATABASE=joomla \
-v "$PWD"/config/mysql/mysql.cnf:/etc/mysql/conf.d/mysql.cnf \
-v "$PWD"/database/mysql:/var/lib/mysql \
--privileged=true \
-p 3306:3306 -d mysql:5.6.36 \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_unicode_ci


# init apache-php
docker run  --name $app \
--link $database:mysql  \
-v "$PWD"/src/:/var/www \
-p 80:80 -d registry.cn-hangzhou.aliyuncs.com/lxepoo/apache-php5

echo "[INFO] 初始化docker运行容器完成"

# 查看状态
docker ps -a 
exit 0;
}


[ "$1" == "rm" ] && { 
docker ps -a 
read -p "[INFO] 删除当前宿主机器上的全部容器?(y/n):" action
  [ "$action" == "y" ] && {
  	docker rm -f $(docker ps -qa)
  }
  exit 0;
}

[ "$1" == "restart" ] && { 
docker ps -a 
echo "[INFO] 重启当前宿主机器上的全部容器?"
read -p "(y/n):" action
  [ "$action" == "y" ] && {
  	docker restart $(docker ps -qa)
  }
  exit 0;
}